package br.com.felix.easyclearcase.ui.dialogs;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import br.com.felix.clearcase.command.ParametrizedCommand;


/**
 * Dialog to execute ParametrizedCommands. 
 * Allow the user to enter the command parameters.
 * */
public class ExecuteCommandDialog extends javax.swing.JDialog {

	private static final long serialVersionUID = 6582888096440563156L;
	
	/**
	 * Map for parameters and its correspondent text fields.
	 * */
	private Map<String,JTextField> mapComponents = new HashMap<String,JTextField>();
	
	private JButton btnOk = new JButton("ok");
	
	/**
	 * Constructor. Initializes the user interface providing fields to specify the command's parameters.
	 * The Ok button is configured to update the parameters values of the parametrized command when clicked.
	 * */
	public ExecuteCommandDialog(final ParametrizedCommand pcmd){
		setTitle("Executing Command "+pcmd.getName());
		
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 2;
		gbc.insets = new Insets(0,0,15,0);
		
		JTextArea txtaMsg = new JTextArea("Please enter the command parameters:");
		txtaMsg.setEditable(false);
		txtaMsg.setBackground(getBackground());
		add(txtaMsg,gbc);
		
		gbc.gridwidth = 1;
		gbc.insets = new Insets(0,0,0,15);
		
		Set<String> parameters = pcmd.getParameters();
		setSize(new Dimension(500,200+(parameters.size()+1)*15));
		
		if(parameters.size()==1){
			txtaMsg.setText("This command has no parameters.");
		}
		
		JTextField txtParameter;
		for(String parameter:parameters){
			if("file".equals(parameter))continue;
			gbc.gridx = 0;
			gbc.gridy++;
			add(new JLabel(parameter),gbc);
			
			txtParameter = new JTextField();
			txtParameter.setColumns(15);
			mapComponents.put(parameter, txtParameter);
			gbc.gridx++;
			add(txtParameter,gbc);
		}
		
		
		
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				dispose();
			}
		});
		
		btnOk.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent e) {
				for(String parameter:mapComponents.keySet()){
					pcmd.setParameter(parameter, mapComponents.get(parameter).getText());
				}
			}
		});
		
		gbc.gridx = 0;
		gbc.gridwidth = 2;
		gbc.gridy++;
		
		JPanel buttonsPanel = new JPanel();
		buttonsPanel.add(btnOk);
		buttonsPanel.add(btnCancel);
		
		add(buttonsPanel,gbc);
	}
	

	/**
	 * Adds a mouse listener to the Ok button.
	 * */
	public void addOkButtonMouseListener(MouseListener ml){
		btnOk.addMouseListener(ml);
	}
}
